// pages/personal/personal.js
const timeUtil = require("../../utils/timeUtil")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageAvator: "../../image/icon_default_avator.png",
    inputValue: "",
    joinDays: 0,
    chatSize: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.initData()
  },

  initData() {
    var it = this
    wx.getStorage({
      key: 'avator',
      success(res) {
        console.log(res.data)
        if (res.data != "") {
          it.setData({
            imageAvator: res.data
          })
        }
      }
    })
    wx.getStorage({
      key: 'nickName',
      success(res) {
        console.log(res.data)
        if (res.data != "") {
          it.setData({
            inputValue: res.data
          })
        }
      }
    })
    wx.getStorage({
      key: 'chatMsg',
      success(res) {
        if (res.data !== "") {
          it.setData({
            chatSize: res.data.length
          })
        }
      }
    })
    wx.getStorage({
      key: 'joinDays',
      success(res) {
        var timeDevi = timeUtil.Jh_getTimeStamp() - res.data;
        var days = timeDevi / 1000 / 60 / 60 / 24;
        it.setData({
          joinDays: Math.ceil(days)
        })
      }
    })
  },
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail 
    this.setData({
      imageAvator:avatarUrl
    })
  },

  bindKeyInput(e) {
    this.setData({
      inputValue: e.detail.value
    })
  },
  aboutUs(){
    wx.showModal({
      title: '关于我们',
      content: '此“开心学学乐”小程序，仅作学习交流使用，不作任何商业盈利，如有侵权，请联系开发者删除。'
    })
  },
  onUnload: function () {
    var it = this
    console.log(it.data.inputValue)
    wx.setStorage({
      key: "nickName",
      data: it.data.inputValue
    });
    wx.setStorage({
      key: "avator",
      data: it.data.imageAvator
    })
  }

})